function [ F_Harmonics, P_Harmonics, t, ydata, z1, y, Yfft, delta_t, L, Tp, ...
    t_P, ydata_P, z1_P, y_P, Yfft_P, delta_t_P, L_P, Tp_P, Pressure, Flow, Impedence ] ...
    = ImpedanceOnlyFunc( inputAxes, SaveLocation, InputFilesLocation, instructionText, ...
    FileTypeInput, percentErrorText, P_ejectionDurationText, F_ejectionDurationText, ...
    uploadFlag, EchoTypeNum )
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here

%% Error Checking and import validation
%%Documentation everything

%% Overall Program 

%% Step 0 Add to path needed functions

cd(InputFilesLocation)

%% Import Error Checking

%Get pressure path
set(instructionText, 'string', 'Please select the file containing the pressure data you wish to use as input')
[filePathPressure, fileName] = checkPressureFileType(FileTypeInput);

% Get flow path (and convert jpeg image to dicom if necessary)
set(instructionText, 'string', 'Please select the file containing the echo data you wish to use as input')
if EchoTypeNum == 0 % dicom image
    [filePathFlow] = getFilePath('*.dcm', 'dicom');
else % jpeg image, must be converted to dicom
    [pathName, fileNameFlow] = getFlowPath('*.jpg', 'jpeg');
    pathsAndNames = cell(1,2);
    pathsAndNames(1) = cellstr(strcat(pathName, fileNameFlow));
    pathsAndNames(2) = cellstr(fileNameFlow(1:end-4));
    saveLocation = pathName(1:end-1);
    writeDicoms( pathsAndNames, saveLocation );
    filePathFlow = strcat(pathName, fileNameFlow(1:end-3)); filePathFlow = strcat(filePathFlow, 'dcm');
end

%% Import Files/Data

% Pressure - will import into object strucutre (.txt) -> Output: Pressure

if FileTypeInput == 0
    PatientID = importfile_PatientID(filePathPressure);
    pressureHeartRate=importfile_HR(filePathPressure);
    pressureWaveformData = importfile_pressure(filePathPressure); % returns just p values
    samplingFrequency = importfile_sampelingfreq(filePathPressure);
    ED = importfile_ED(filePathPressure);
    visitDate = 0
    visitTime = 0

elseif FileTypeInput == 1
    
    [NUM,TXT,RAW]=xlsread(filePathPressure);
    PatientID = char(TXT(1));
    try %Accounts for inconsistant formatting of the xls files
        pressureHeartRate = cell2mat(TXT(4)); pressureHeartRate = pressureHeartRate(4:5); pressureHeartRate = str2num(pressureHeartRate);
    catch ME 
        pressureHeartRate = cell2mat(TXT(6)); pressureHeartRate = pressureHeartRate(4:5); pressureHeartRate = str2num(pressureHeartRate);
    end
    pressureWaveformData = NUM;
    SAMPLING_FREQUENCY = 128;

elseif FileTypeInput == 2
    
    [PatientID, visitDate, visitTime] = parseFileName(fileName);
    try
        yamlFilePath = getappdata(0, 'yamlFilePath');
    
    % Extract metadata necessary to query redcap for patient heart rate    
    [ eventName, sampleNumber ] = getInstrumentMetadata( char(PatientID), visitDate, ...
                                                         visitTime, yamlFilePath );
                                                     
                                                     
    [ pressureHeartRate ] = extractRedcapHeartRate( char(PatientID), eventName, sampleNumber, yamlFilePath );

    catch ME
        
        SAMPLING_FREQUENCY = 128;
        [ pressureHeartRate, pressureWaveformData ] = computepressureheartrate(filePathPressure, SAMPLING_FREQUENCY, instructionText);
        
    end
        
end


Pressure.PatientID = PatientID;
Pressure.HR = pressureHeartRate;
Pressure.InputData = pressureWaveformData;
try
    Pressure.SamplingFreq = samplingFrequency;
catch ME
    Pressure.SamplingFreq = SAMPLING_FREQUENCY;
end
Pressure.MeasurementDate = visitDate;
Pressure.MeasurementTime = visitTime;

if FileTypeInput == 0
    Pressure.ED = ED;
end

clear age

% Flow - will import dicom file (.dcm) into Object

dicomImg = dicomread(filePathFlow);

[SaveLocation] = createFigureDirectory(SaveLocation, 'ImpedanceData');
 
%% Step 1 Import and Process Pressure Data

[TimeOriginal, TimeFinal, FinalPressureValues, SignalPeriod] = sphygmaCorDataProcessing(Pressure.InputData, Pressure.SamplingFreq, ...  
                                                    Pressure.HR);

Pressure.OutputData = [TimeFinal, FinalPressureValues];
Pressure.Period = SignalPeriod;
Pressure.InputData = [TimeOriginal, Pressure.InputData];

clear TimeOriginal TimeFinal FinalPressureValues SignalPeriod

%% Step 2 Import and Process Flow Data

[Flow] = dicomAnalysisGUI(dicomImg, inputAxes, instructionText);

% Save diagnostic figures

saveas(Flow.sampledEcgPlot, strcat(SaveLocation, '/', 'SampledEcgOverlay.jpg'));
saveas(Flow.ecgGatedVelPlot, strcat(SaveLocation, '/', 'SampledVelocityOverlay.jpg'));


%% Step 3 Perform heart rate error checking

Flow = getFlowHeartRate(filePathFlow, Flow, dicomImg, inputAxes, instructionText);

heartRateDifference = findHeartRateDiff(Flow.HR, Pressure.HR);

%% Step 4 Caclulate FFT of both Pressure and Flow waveforms

% Pressure FFT

[Pressure.FFT_Outputs, Pressure.FigFFT, t_P, ydata_P, z1_P, y_P, Yfft_P, delta_t_P, L_P, Tp_P] = fftGeneralWithMoreOutputs(Pressure.OutputData(:,2), Pressure.OutputData(:,1), 'Pressure');
saveas(Pressure.FigFFT, strcat(SaveLocation, '/', 'FFT_PressureSanityCheckPlot.jpg'));


% Flow FFT
[Flow.FFT_Outputs, Flow.FigFFT, t, ydata, z1, y, Yfft, delta_t, L, Tp] = fftGeneralWithMoreOutputs(Flow.velocity, Flow.time, 'Flow');
saveas(Flow.FigFFT, strcat(SaveLocation, '/', 'FFT_FlowSanityCheckPlot.jpg'));

figPF = figure; set(figPF, 'Visible', 'off');
[AX,H1,H2] = plotyy(Pressure.OutputData(:,1), Pressure.OutputData(:,2), Flow.time, Flow.velocity);
xlabel('Time (sec)'); ylabel(AX(1),'Pressure (dynes/cm^2)'); ylabel(AX(2),'Flow (cm/sec)');
saveas(figPF, strcat(SaveLocation, '/', 'Pressure&FlowSanityCheckPlot.jpg'))

% Interpolate Flow FFT data using Pressure frequency data
[ Flow ] = interpolateFFT( Flow, Pressure );

%% Step 5 Cacluate Impedence at Each Harmonic
P_Harmonics = Pressure.FFT_Outputs;
F_Harmonics = Flow.FFT_Interp;

[ Impedence ] = ImpedenceCalcGUI(P_Harmonics, F_Harmonics, true ); % True will Output figure as well

saveas(gcf,strcat(SaveLocation, '/', 'Impedence.jpg'))


%% Calculate difference in ejection duration

if FileTypeInput == 0
    %Find time difference between the first two 
    zeroIdx = find(Flow.velocity == 0);
    ejectionDuration = abs(Flow.time(zeroIdx(1,1),1) - Flow.time(zeroIdx(2,1),1));
    percentError = abs(100*(ejectionDuration - Pressure.ED)/ejectionDuration);
    set(percentErrorText, 'string', percentError);
    set(P_ejectionDurationText, 'string', Pressure.ED);
    set(F_ejectionDurationText, 'string', ejectionDuration);
end

%% Step 7 Export Data

if uploadFlag == 1
    
    
    yamlFilePath = getappdata(0, 'yamlFilePath');

    % Extract metadata necessary to query redcap for patient heart rate  
    [ eventName, sampleNumber ] = getInstrumentMetadata( char(Pressure.PatientID), char(Pressure.MeasurementDate), ...
                                                         visitTime, yamlFilePath );
                                        
    % Extract the first 15 (and zeroth) harmonics and upload the data to Redcap
    setAndUploadToRedcap( char(Pressure.PatientID), Impedence, eventName, sampleNumber, yamlFilePath );


end

% Add Save Location for output of data
csvwrite(strcat(SaveLocation, '/', 'Impedence.csv'), Impedence);

% create matrix of flow and pressure data for export
%Scaled_Waveforms = [Flow.time, Flow.velocity, Pressure.OutputData(:,1), Pressure.OutputData(:,2)];

%csvwrite(strcat(SaveLocation, '/', 'ScaledWaveforms.csv'), Scaled_Waveforms);

% Write out Processed Pressure and Flow Waveforms (uncompressed)

% Flow
csvwrite(strcat(SaveLocation, '/', 'Flow_uncompressed.csv'), [Flow.time, Flow.velocity]);

% Pressure
csvwrite(strcat(SaveLocation, '/', 'Pressure_uncompressed.csv'), Pressure.OutputData);

% Individual Impedance Harmonics
csvwrite(strcat(SaveLocation, '/', 'PressureFlowHarmonics.csv'), [P_Harmonics, F_Harmonics]);





end
% [ eventName, sampleNumber ] = getInstrumentMetadata( char(PatientID), visitDate, visitTime, yamlFilePath )
